import requests
from bs4 import BeautifulSoup
import time
import random


# 在异步请求里找到需要的信息
def get_html(jsondata):
    if jsondata.get('data'):
        data = jsondata.get('data')
        # yield {
        #     data.get('list'),
        # }
        return data.get('list')


# 异步请求url
def get_page(url):
    try:
        r = requests.get(url)
        if r.status_code == 200:
            # 返回json格式的响应内容
            return r.json()
    except:
        return None


# 解析json返回的内容
def get_soup(url):
    jsondata = get_page(url)
    return get_html(jsondata)


# 不同浏览器访问
uas = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/73.0.3683.103 Safari/537.36",\
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134",\
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"]


# 拼接url
def splicing_url(page: int,size: int):
    url = ['https://www.taptap.com/webapiv2/landing/v5/timeline-with-guest', '?action=refresh']
    token = '&X-UA=V%3D1%26PN%3DWebApp%26LANG%3Dzh_CN%26VN_CODE%3D91%26VN%3D0.1.0%26LOC%3DCN%26PLT%3DPC%26DS%3DAndroid%26UID%3Ddf8eea9e-d48d-4b94-994f-a17aa5c3c108%26DT%3DPC%26OS%3DWindows%26OSV%3D10.0.0'
    if page != 1:
        t = str(time.time()).split('.')[0]
        limit = str(size)
        f = str((page-1)*size)
        url.append('&time='+t)
        url.append('&filter_data=')
        url.append('&limit='+limit)
        url.append('&from='+f)
    url.append(token)
    return ''.join(url)


# 拼接折线图数据url
def line_chart_url(game_id):
    url = ['https://www.taptap.com/webapiv2/review/v1/trend','?app_id='+str(game_id),'&X-UA=V%3D1%26PN%3DWebApp%26LANG%3Dzh_CN%26VN_CODE%3D3%26VN%3D0.1.0%26LOC%3DCN%26PLT%3DPC%26DS%3DAndroid%26UID%3D54f3fea2-ce85-4bbd-8a58-ef29a1263af9%26DT%3DPC']
    return ''.join(url)


# 爬取网页上的信息
def agame(url):
    gamesDetail = {}
    headers = {'user-agent': get_ua()}
    res = requests.get(url, headers=headers)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    # 游戏厂商
    gamesDetail['game_manufacturer'] = soup.select('.header-text-author')[0].select('span')[1].text
    # 游戏分类
    if soup.select('#appTag') is None:
        gamesDetail['game_tab'] = ''
    else:
        gamesDetail['game_tab'] = ' '.join(soup.select('#appTag')[0].text.lstrip().rstrip().split()[:3])
    # 更新时间
    gamesDetail['game_date'] = soup.select('.info-item-content')[2].text
    # 游戏简介
    gamesDetail['game_brief_introduction'] = soup.select('#description')[0].text.lstrip().rstrip()
    return gamesDetail


# 设置合理的爬取间隔
def sleep():
    for i in range(5):
        # 沉睡随机数的3倍秒数
        time.sleep(random.random()*3)


# 随机选择user-agent
def get_ua():
    au = random.choice(uas)
    return au


def get_list(pageSize):
    games = []
    for i in range(pageSize):
        url = splicing_url((i+1), 10)
        print(url)
        # ls = get_soup(url)
        # for l in ls:
        #     if l.get('type') == ('default'):
        #         des_url = 'https://www.taptap.com'
        #         des_url = des_url+l.get('web_url')
        #         gamesDetail = agame(des_url)
        #         gamesDetail['game_id'] = l.get('app_summary').get('id')
        #         gamesDetail['game_name'] = l.get('title')
        #         gamesDetail['game_score'] = l.get('rating').get('score')
        #         games.append(gamesDetail)
    return games


def get_line_chart_data(game_id):
    url = line_chart_url(game_id)
    headers = {'user-agent': get_ua()}
    ret = requests.get(url,headers=headers)
    retJson = ret.json()
    list = retJson.get('data').get('trend')
    trends = []
    for l in list:
        date = l.get('date')
        dates = date.split('-')
        del(dates[0])
        trend = {'date': '-'.join(dates),
                 'positive_count': l.get('positive_count'),
                 'negative_count': l.get('negative_count')}
        trends.append(trend)
    return trends
